//时间区间验证
function checkTime(startTime,endTime){
	var startTime = startTime.replace(/-/g,"/");
	var endTime = endTime.replace(/-/g,"/");
	if(new Date(startTime).getTime() > new Date(endTime).getTime()){
		BSTYUN.dialog.warn('请填写正确的时间区间！');
		return false;
	}else{
		return true;
	}
}
//发票信息列表
function initInvoiceDetail() {
	var startTime = $.trim( $('#startTime').val() );
	var endTime = $.trim( $('#endTime').val() );
	if(!checkTime(startTime,endTime)) return; 
	$.ajax({
		type : "post",
		url : "/finance/invoice/invoiceList",
		data : {
			startTime : startTime,
			endTime : endTime
		},
		dataType : "json",
		success : function(data) {
			$("#invoiceDetailList0").empty();
			var tb_content = '';
			if (data.invoicesList.length > 0) {
				$.each(data.invoicesList, function(index, invoice) {
					tb_content += '<tr id="' + invoice.invoiceId
							+ '"><td style="border: 0px">' + '<font>'
							+ toDate(invoice.addTime, "yyyy-MM-dd hh:mm:ss")
							+ '</font></td><td style="border: 0px"><font>'
							+ invoice.invoiceTitle
							+ '</font></td><td style="border: 0px"><font>'
							+ parseFloat(invoice.invoiceMoney).toFixed(2)
							+ '</font></td><td style="border: 0px">' + '<font>'
							+ checkInvoiceState(invoice.invoiceState)
							+ '</font></td>' + '<td style="border: 0px"><font>'
							+'<a href="javascript:lookDetail('+invoice.invoiceId+')">查看详情</a></font></td></tr>'
				})
			}else{
				tb_content += '<tr><td colspan="5">查无记录</td></tr>';
			}
		$("#invoiceDetailList0").append(tb_content);
		},
	});
}
//待邮寄发票信息列表
function initInvoicePart() {
	var startTime = $.trim( $('#moneyStartTime').val());
	var endTime = $.trim( $('#moneyEndTime').val() );
	var invoiceState = $.trim( $('#invoiceState').val() );
	var dealTab = $("#invoiceDetailList1");
	if(!checkTime(startTime,endTime)) return; 
	$.ajax({
		type : "POST",
		url : "/finance/invoice/invoiceList",
		data : {
			startTime : startTime,
			endTime : endTime,
			invoiceState : 1 
		},
		success : function(data, response) {
			$("#invoiceDetailList1").empty();
			var tb_content = '';
			if (data.invoicesList.length > 0) {
				$.each(data.invoicesList, function(index, invoice) {
					tb_content += '<tr id="' + invoice.invoiceId
							+ '"><td style="border: 0px">' + '<font>'
							+ toDate(invoice.addTime, "yyyy-MM-dd hh:mm:ss")
							+ '</font></td><td style="border: 0px"><font>'
							+ invoice.invoiceTitle
							+ '</font></td><td style="border: 0px"><font>'
							+ parseFloat(invoice.invoiceMoney).toFixed(2)
							+ '</font></td><td style="border: 0px"><font>'
							+ checkInvoiceState(invoice.invoiceState)
							+ '</font></td>' + '<td style="border: 0px"><font>'
							+'<a href="javascript:lookDetail('+invoice.invoiceId+')">查看详情</a></font></td></tr>'
				})
			}else{
				tb_content += '<tr><td colspan="5">查无记录</td></tr>';
			}
			$("#invoiceDetailList1").append(tb_content);
		}
	})
}
// 是否为空判断
function isNull(data) {
	if (data == null) {
		return "";
	} else {
		return data;
	}
}
// 收取方式显示
function checkTakeWay(takeWay) {
	if (takeWay != null && takeWay != '') {
		if (takeWay == "0") {
			return "邮寄";
		} else if (takeWay == "1") {
			return "自取";
		} 
	} else {
		return "";
	}
}
//发票状态显示
function checkInvoiceState(invoiceState) {
	if (invoiceState == 0) {
		return "未索取";
	} else if (invoiceState == 1) {
		return "待邮寄";
	} else if (invoiceState == 2) {
		return "已邮寄";
	}
}

$(function() {
	//初始化查询发票列表
	initInvoiceDetail();
	//绑定返回收支明细事件
	var $invoice_list = $(".invoice_list"),
		$invoice_detail = $(".invoice_detail"),
		$return = $("#returnInvoiceList");
	$return.on("click",function(){
		$invoice_list.show();
		$invoice_detail.hide();
		$return.hide();
	});
	//绑定tab点击事件
	$("#myTab").on("click","li",function(){
		$return.trigger("click");
	});
})

//查看发票详情
function lookDetail(invoiceId){
	var $invoice_list = $(".invoice_list"),
		$invoice_detail = $(".invoice_detail"),
		$return = $("#returnInvoiceList");
	$.ajax({
		type : "POST",
		url : "/finance/invoice/invoiceDetail/"+invoiceId,
		success : function(data) { 
			var invoice = data.invoice;
			//详细card展现
			$invoice_list.hide();
			$invoice_detail.show();
			$return.show();
			//放数据
			$('#invoiceTitle1').text(invoice.invoiceTitle);
			$('#openType').text(checkOpenType(invoice.openType));
			//$('#openType').text(invoice.openType);
			$('#invoiceType').text(checkInvoiceType(invoice.invoiceType));
			$('#invoiceContent').text(isNull(invoice.invoiceContent));
			var addTime = new Date(invoice.addTime);
			addTime = toDate(addTime, "yyyy-MM-dd hh:mm:ss");
			$('#addTime').text(addTime);
			$('#invoiceState1').text(checkInvoiceState(invoice.invoiceState));
			$('#invoiceMoney').text(parseFloat(invoice.invoiceMoney).toFixed(2));
			$('#receiverName').text(isNull(invoice.receiverName));
			$('#userTell').text(isNull(invoice.userTell));
			$('#address').text(invoice.provinceCodeName+invoice.cityCodeName+invoice.areaCodeName+invoice.streetAddress);
			$('#deliveryType').text(isNull(invoice.deliveryType));
			$('#deliveryNum').text(isNull(invoice.deliveryNum));
		},
		error: function(){
			BSTYUN.dialog.warn("网络异常！请稍后再试")
		}
	});
}

// 获取开具类型：
function checkOpenType(openType) {
	if (openType == 0) {
		return "个人";
	} else if (openType == 1) {
		return "企业";
	}
}
// 获取发票类型：
function checkInvoiceType(invoiceType) {
	if (invoiceType == 0) {
		return "增值税专票";
	} else if (invoiceType == 1) {
		return "增值税普票";
	}
}

// tab切换
function nTabs(thisObj, Num) {
	if ('0' == Num) {
		$("#myTab_Content0").css("display", "block");
		$("#myTab_Content1").css("display", "none");
		$(thisObj).attr("class", "active");
		$(thisObj).next().attr("class", "sta");
		//发票列表显示
		initInvoiceDetail();
	} else {
		$("#myTab_Content0").css("display", "none");
		$("#myTab_Content1").css("display", "block");
		$(thisObj).attr("class", "active");
		$(thisObj).prev().attr("class", "sta");
		//待邮寄发票列表显示
		initInvoicePart();
	}
}